﻿using System;
using System.Linq;
using System.Windows.Forms;
using ConfigurationManagement.Administration.Data;
using ConfigurationManagement.Data;
using Environment = ConfigurationManagement.Data.Environment;

namespace ConfigurationManagement.Administration.UI.Utilities
{
    public partial class CopyEnvironmentForm : Form
    {
        private readonly ConfigurationManagementDataContext _db = new ConfigurationManagementDataContext();
        private Environment _selectedEnvironment;
        private Project _selectedProject;

        public CopyEnvironmentForm()
        {
            InitializeComponent();
        }

        #region Event Handlers

        private void ProjectsComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ProjectsComboBox.SelectedIndex < 0) return;
            _selectedProject = (Project) ProjectsComboBox.SelectedItem;
            LoadEnvironments();
        }

        private void SolutionsComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            _selectedEnvironment = (Environment) EnvironmentsComboBox.SelectedItem;
        }

        private void CopySolutionFormLoad(object sender, EventArgs e)
        {
            LoadProjects();
        }

        private void CopySolutionButtonClick(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(NewNameTextBox.Text))
            {
                MessageBox.Show(@"Please supply a new environment name");
                return;
            }

            if (_selectedProject == null)
            {
                MessageBox.Show(@"Please select a project");
                return;
            }
            if (_selectedEnvironment == null)
            {
                MessageBox.Show(@"Please supply an environment");
                return;
            }
            if (_selectedProject.Environments.Count(p => p.EnvironmentName == NewNameTextBox.Text) > 0)
            {
                MessageBox.Show(@"An environment with this name already exists");
                return;
            }

            try
            {
                CopyEnvironment();
                MessageBox.Show(@"The environment has been copied");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        private void LoadProjects()
        {
            ProjectsComboBox.DisplayMember = "ProjectName";
            ProjectsComboBox.ValueMember = "ProjectID";
            ProjectsComboBox.DataSource = _db.Projects;
            _selectedProject = (Project) ProjectsComboBox.SelectedItem;
            LoadEnvironments();
        }

        private void LoadEnvironments()
        {
            if (_selectedProject == null) return;
            EnvironmentsComboBox.DisplayMember = "EnvironmentName";
            EnvironmentsComboBox.ValueMember = "EnvironmentID";
            EnvironmentsComboBox.DataSource = _selectedProject.Environments;
            _selectedEnvironment = (Environment) EnvironmentsComboBox.SelectedItem;
        }

        private void CopyEnvironment()
        {
            var newEnvironment = new Environment {EnvironmentName = NewNameTextBox.Text, EnvironmentDEscription = ""};
            for (var index = 0; index < _selectedEnvironment.SettingValues.Count; index++)
            {
                var settingValue = _selectedEnvironment.SettingValues[index];
                var newSettingValue = new SettingValue
                                          {
                                              SettingValue1 = settingValue.SettingValue1,
                                              SolutionSetting = settingValue.SolutionSetting
                                          };
                newEnvironment.SettingValues.Add(newSettingValue);
            }
            _selectedProject.Environments.Add(newEnvironment);
            if (DataContext.ContainsChanges(_db))
                _db.SubmitChanges();
        }
    }
}